#ifndef REALTIMECANDLESTICKCHART_H
#define REALTIMECANDLESTICKCHART_H

#include <QWidget>
#include <QWidget>
#include <QtCharts/QChart>
#include <QtCharts/QCandlestickSeries>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QCheckBox>
#include <QValueAxis>
#include <QVector>
#include <QPinchGesture>
#include <QStandardItemModel>
#include "binanceklineclient.h"
QT_BEGIN_NAMESPACE
class QComboBox;
class QLabel;
class QPushButton;
class QSlider;
class QTableWidget;
class QAbstractAxis;
// 前向声明：图表悬停所需的图形项
class QGraphicsLineItem;
class QGraphicsRectItem;
class QGraphicsSimpleTextItem;
QT_END_NAMESPACE

QT_USE_NAMESPACE

    class RealTimeCandlestickChart : public QWidget
{
    Q_OBJECT

public:
    explicit RealTimeCandlestickChart(QWidget *parent = nullptr);
    ~RealTimeCandlestickChart();

    void startRealTimeData(const QString& symbol = "btcusdt", const QString& interval = "1m");
    void stopRealTimeData();

private slots:
    void onKLineDataReceived(const KLineData& data);
    void onKLineUpdated(const KLineData& data);
    void onHistoricalDataReceived(const QVector<KLineData>& data);
    void onConnected();
    void onDisconnected();
    void onErrorOccurred(const QString& error);

    void onSymbolChanged();
    void onIntervalChanged();
    void onConnectClicked();
    void onDisconnectClicked();

    // 缩放控制
    void onZoomIn();
    void onZoomOut();
    void onResetZoom();
    void onZoomSliderChanged(int value);

private:
    void setupUI();
    void setupChart();
    void setupConnections();
    void initializeSymbols();

    // 缩放和拖拽功能
    void setupZoomAndDrag();
    void updateZoom();
    void updateChartRange();
    void adjustCandleWidth(double factor);
    void panChart(const QPoint &delta);

    // 事件处理
    bool eventFilter(QObject *obj, QEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void pinchTriggered(QPinchGesture *gesture);

    // 数据管理
    void addKLineToChart(const KLineData& data);
    void updateKLineInChart(const KLineData& data);
    void updateLatestKLine(const KLineData& data);
    void restartRealTimeData();
    void calculatePriceRange();

    BinanceKLineClient *m_client;

    // UI组件
    QChart *m_chart;
    QCandlestickSeries *m_series;
    QChartView *m_chartView;
    //QTableWidget *m_infoTable;
    QComboBox *m_symbolCombo;
    QComboBox *m_intervalCombo;
    QPushButton *m_connectBtn;
    QPushButton *m_disconnectBtn;
    QPushButton *m_zoomInBtn;
    QPushButton *m_zoomOutBtn;
    QPushButton *m_resetZoomBtn;
    QSlider *m_zoomSlider;
    QLabel *m_statusLabel;
    QLabel *m_priceLabel;
    QLabel *m_zoomLabel;
    QLabel *m_dataCountLabel;

    // 数据
    QVector<KLineData> m_klineData;
    KLineData m_currentKLine;
    QStringList m_categories;
    QMap<QString, QString> m_symbolMap;

    // 缩放和拖拽状态
    double m_zoomFactor;
    double m_candleWidth;
    bool m_isPanning;
    QPoint m_lastPanPoint;
    int m_visibleBars;
    bool m_isRealTime;
    // 当前可见数据起点（相对于 m_klineData）
    int m_startIndex;

    // 价格范围
    double m_minPrice;
    double m_maxPrice;

    // 指标系列
    QLineSeries *m_smaSeries;
    QLineSeries *m_emaSeries;
    QLineSeries *m_upperBBSeries;
    QLineSeries *m_lowerBBSeries;
    QLineSeries *m_macdSeries;
    QLineSeries *m_macdSignalSeries;
    QLineSeries *m_rsiSeries;
    QValueAxis *m_rsiAxis;

    // 指标显示开关
    QCheckBox *m_chkSMA;
    QCheckBox *m_chkEMA;
    QCheckBox *m_chkBB;
    QCheckBox *m_chkRSI;
    QCheckBox *m_chkMACD;

    // 计算指标
    void calculateIndicators();
    QVector<double> calculateSMA(int period) const;
    QVector<double> calculateEMA(int period) const;
    QVector<double> calculateRSI(int period) const;
    void calculateMACD(int fastPeriod, int slowPeriod, int signalPeriod, QVector<double> &macdOut, QVector<double> &signalOut) const;
    void calculateBollinger(int period, double devFactor, QVector<double> &upper, QVector<double> &lower) const;
    void refreshIndicatorSeries();
    void toggleIndicatorSeries(QLineSeries *series, bool visible, QAbstractAxis *axis = nullptr);

    // 悬停信息显示（在 K 线上悬停显示信息）
    void updateHoverInfo(int index); // 在指定的 K 线索引位置显示信息
    void clearHoverInfo();           // 清除悬停显示

    // 图表内悬停图形项（十字线与信息框），在 cpp 中初始化并在 updateHoverInfo/clearHoverInfo 中使用
    QGraphicsLineItem *m_vLine;
    QGraphicsLineItem *m_hLine;
    QGraphicsRectItem *m_infoBox;
    QGraphicsSimpleTextItem *m_infoText;

private slots:
    void onIndicatorToggled(bool checked);
private:
    // 指标缓存值（与 m_klineData 对齐，长度相同）
    QVector<double> m_smaValues;
    QVector<double> m_emaValues;
    QVector<double> m_upperBBValues;
    QVector<double> m_lowerBBValues;
    QVector<double> m_macdValues;
    QVector<double> m_macdSignalValues;
    QVector<double> m_rsiValues;


};


#endif // REALTIMECANDLESTICKCHART_H
